using System;
using SMS.SMPPClient.PDU;
using System.Collections;

namespace SMS.SMPPClient.PDU.Outgoing
{
    /// <summary>
    /// This class defines a query_sm ESME originated PDU.
    /// </summary>
    public class SMPPQuerySM : MessageLCD6
    {
        private string _MessageID;

        /// <summary>
        /// Creates a query_sm PDU.
        /// </summary>
        /// <param name="messageID">The ID of the message.</param>
        /// <param name="sourceAddrTON">Type of Number. This must match that supplied in
        /// the original request.</param>
        /// <param name="sourceAddrNPI">Numbering Plan Identity. This must match that
        /// supplied in the original request.</param>
        /// <param name="sourceAddress">Address of message originator.  This must match
        /// that supplied in the original request. If not known, set to null.</param>
        public SMPPQuerySM(string messageID, TONType sourceAddrTON, NPIType sourceAddrNPI,
            string sourceAddress)
            : base(sourceAddrTON, sourceAddrNPI,
            sourceAddress)
        {
            if (messageID != null)
            {
                if (messageID.Length <= MSG_LENGTH)
                    _MessageID = messageID;
                else
                    throw new ArgumentOutOfRangeException(
                        "Message ID must be <= " + MSG_LENGTH + " characters.");
            }
            else
                _MessageID = "";

            _CommandStatus = 0;
            _CommandID = CommandIDType.query_sm;

        }

        ///<summary>
        /// Gets the hex encoding (big-endian) of this PDU.
        ///</summary>
        ///<return>The hex-encoded version of the PDU</return>
        public override System.Byte[] GetMSBHexEncoding()
        {
            ArrayList pdu = GetPDUHeader();

            pdu.AddRange(ArrayCopyWithNull(
                System.Text.Encoding.ASCII.GetBytes(_MessageID)));

            pdu.Add((byte)_SourceAddrTON);
            pdu.Add((byte)_SourceAddrNPI);

            pdu.AddRange(ArrayCopyWithNull(
                System.Text.Encoding.ASCII.GetBytes(_SourceAddress)));

            return EncodePDUForTransmission(pdu);
        }
    }
}

